<?php
require_once("http.php");

function getHtml($url){
	$oHTTP = new HTTP();
	$oHTTP->setURL($url);
	$oHTTP->setMethod("GET");
	$oHTTP->setM_referer("http://www.sina.com.cn");
	$oHTTP->addParameter("param","value");
	$oHTTP->addFile("filedata","/tmp/tmpFile");
	if( !$oHTTP->sendRequest() ){
	    print($oHTTP->getError());
	    exit();
	}
	$request  = $oHTTP->getRequest();
	$response = $oHTTP->getResponse();
	$response = str_replace(array("\n\r","\n","\r"), "", $response);
	return $response;
}

function save($content) {
	$log = fopen ('/mnt/hgfs/zendworkspace/spider/fn/nn.html', 'ab');
	flock ($log, LOCK_EX);
	fwrite ($log, $content);
	flock ($log, LOCK_UN);
	fclose ($log);
}

function getContent($link){
	$body = getHtml($link);
	preg_match_all("/<div class=\"authi\">.*?<a.+?class=\"xw1\">(.+?)<\/a>.*?<div class=\"t_fsz\">.*?<td.*?id=\"postmessage_\d{7}\".*?>(.*?)<\/td>/", $body, $matches);
	$content = "";
	$comment_arr = array();
	for ($i=0; $i< count($matches[0]); $i++) {
		if($i == 0) {
			$content = $matches[2][$i];
		}else {
			$author = $matches[1][$i];
			$comment = $matches[2][$i];
			echo "content:$content, author:$author, comment:$comment \n\n";
		}
		
	}
}

function getBBSInfo($url){
	$response = getHtml($url);
	preg_match_all ("/<tbody id=\"normalthread_\d{6}\">.*?<th class=\"new\"><em>\[<a.*?>(.+?)<\/a>\]?<\/em>.+?<a.+?href=\"(.+?)\".+?>(.+?)<\/a>.*?<td class=\"by\">.+?<a.+?>(.+?)<\/a>/", $response, $matches);
	for ($i=0; $i< count($matches[0]); $i++) {
		$cat = $matches[1][$i];
		$link = $matches[2][$i];
		$link = urldecode($link);
		$link = htmlspecialchars_decode($link);
		$title = $matches[3][$i];
		$author = $matches[4][$i];
		echo "cat:$cat,link:$link, title:$title, author:$author \n\n";
		getContent($link);
	}
}

$url = "http://bbs.jsfuning.com/forum.php?mod=forumdisplay&fid=10&filter=author&orderby=dateline";
getBBSInfo($url);